package com.tjetc.impl;

import java.util.List;

import com.tjetc.dao.CommunityeventMapper;
import com.tjetc.domian.Communityevent;
import com.tjetc.service.CommunityeventService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

public class CommunityeventServiceImpl implements CommunityeventService {
    private static CommunityeventMapper communityeventMapper = Db.getMapper(CommunityeventMapper.class);
    @Override
    public List<Communityevent> list() {
       return communityeventMapper.select();
    }

    @Override
    public Result add(Communityevent communityevent) {
        if (communityevent!=null){
           if (communityeventMapper.findByName(communityevent.getEventname())!=null){
               return new Result(500,"该活动已存在",null);
           }else{
               int i = communityeventMapper.insert(communityevent);
               int j =communityeventMapper.createTable(communityevent.getEventid());
               return (i==1&&j==0)?new Result(200,"添加成功",communityevent):new Result(500,"添加失败",null);

           }
            }else {
            return new Result(500,"添加内容不能为空",null);
        }
        }


    @Override
    public Result update(Communityevent communityevent) {
        if (communityevent!=null){
            int i = communityeventMapper.updateByPrimaryKey(communityevent);
            return i==1?new Result(200,"修改成功",communityevent):new Result(500,"修改失败",null);
        }else {
            return new Result(500,"修改内容不能为空",null);
        }

    }

    @Override
    public Result del(int id) {
       if (communityeventMapper.findById(id)!=null){
           int i = communityeventMapper.deleteByPrimaryKey(id);
           return i==1?new Result(200,"删除成功",null):new Result(500,"删除失败",null);
       }else {
           return new Result(500,"删除内容不存在",null);
       }
    }

    @Override
    public Result join(int eventId, String name) {
        if (communityeventMapper.findById(eventId)!=null){
            int i = communityeventMapper.insertevent(eventId,name);
            return i==1?new Result(200,"报名成功",null):new Result(500,"报名失败",null);
        }else {
            return new Result(500,"活动不存在",null);
        }
    }

    @Override
    public Result cancel(int eventId, String name) {
        if (communityeventMapper.findByUserName(eventId,name)){
            int i = communityeventMapper.deleteByName(eventId,name);
            return i==1?new Result(200,"取消成功",null):new Result(500,"取消失败",null);
        }else {
            return new Result(500,"未查询到报名信息",null);
        }
    }

    @Override
    public List<Communityevent> findByEventNameLike(String eventName) {
        return communityeventMapper.findByEventNameLike(eventName);
    }

    @Override
    public List<Communityevent> findByEventDate(String eventDate) {
        return communityeventMapper.findByEventDate(eventDate);
    }

    @Override
    public List<Communityevent> findByDateRange(String startDate, String endDate) {
        return communityeventMapper.findByDateRange(startDate, endDate);
    }

    @Override
    public List<Communityevent> findByLocationLike(String location) {
        return communityeventMapper.findByLocationLike(location);
    }
}
